<template>
  <div>
    订单信息
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="25">
        <!-- 搜索区域     -->
        <el-col :span="10">
          <el-input
            placeholder="请输入搜索内容"
            clearable
            @clear="allOrder"
            v-model="queryInfo.query"
          >
            <template #append>
              <el-button icon="el-icon-search" @click="allOrder"></el-button>
            </template>
          </el-input>
        </el-col>
        <el-col :span="4">
          <!-- 搜索按钮 -->
          <el-button type="primary" @click="addDialogVisible = true"
            >添加新订单
          </el-button>
        </el-col>
      </el-row>

      <!-- 订单列表 border边框，stripe隔行变色 -->
      <el-table :data="orderList" border stripe>
        <!-- 索引列 -->
        <el-table-column type="index"> </el-table-column>
        <el-table-column prop="userId" label="用户编号"> </el-table-column>
        <el-table-column prop="fieldId" label="场地编号"> </el-table-column>
        <el-table-column prop="clothesId" label="服装编号"> </el-table-column>
        <el-table-column prop="utilId" label="道具编号"> </el-table-column>
        <el-table-column prop="total" label="总价"> </el-table-column>
        <!-- 作用域插槽 -->
        <!-- <template v-slot="scope"> -->
        <!-- {{ scope.row }} 每一行封存的数据 -->
        <!-- <el-switch -->
        <!-- v-model="scope.row.state" -->
        <!-- @change="orderStateChange(scope.row)" -->
        <!-- ></el-switch> -->
        <!-- </template> -->

        <el-table-column label="操作">
          <template v-slot="scope">
            <!-- 修改 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="updateVisible(scope.row)"
            ></el-button>
            <!-- 删除 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteOrder(scope.row.orderId)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页部分 size-change 每页最大变化 current-change 当前最大变化 layout功能组建-->
      <div>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pageNum"
          :page-sizes="[1, 2, 5, 100]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
    <!-- 新增订单区域 -->
    <el-dialog
      title="添加新订单"
      v-model="addDialogVisible"
      width="50"
      @close="addDialogClosed"
    >
      <el-form
        :model="addForm"
        :rules="addFormrules"
        ref="addFormRef"
        label-width="80px"
      >
        <el-form-item label="用户编号" prop="userId">
          <el-input v-model="addForm.userId"></el-input>
        </el-form-item>
        <el-form-item label="场地编号" prop="fieldId">
          <el-input v-model="addForm.fieldId"></el-input>
        </el-form-item>
        <el-form-item label="服装编号" prop="clothesId">
          <el-input v-model="addForm.clothesId"></el-input>
        </el-form-item>
        <el-form-item label="道具编号" prop="utilId">
          <el-input v-model="addForm.utilId"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="addOrder">确定</el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 修改订单区域 -->
    <el-dialog
      title="修改订单"
      v-model="updateDialogVisible"
      width="50"
      @close="updateDialogClosed"
    >
      <el-form
        :model="updateForm"
        :rules="updateFormrules"
        ref="updateFormRef"
        label-width="80px"
      >
        <el-form-item label="用户编号" prop="userId">
          <el-input v-model="updateForm.userId"></el-input>
        </el-form-item>
        <el-form-item label="场地编号" prop="fieldId">
          <el-input v-model="updateForm.fieldId"></el-input>
        </el-form-item>
        <el-form-item label="服装编号" prop="clothesId">
          <el-input v-model="updateForm.clothesId"></el-input>
        </el-form-item>
        <el-form-item label="道具编号" prop="utilId">
          <el-input v-model="updateForm.utilId"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="updateDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="updateOrder">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      //查询信息实体
      queryInfo: {
        query: "", //查询信息
        pageNum: 1, //当前页
        pageSize: 5, //每页的最大值
      },
      orderList: [], //订单列表
      total: 0, //总记录数
      addDialogVisible: false, //对话框隐藏显示状态
      updateDialogVisible: false,
      //添加表单信息
      addForm: {
        userId: "",
        orderId: "",
        clothesId: "",
        utilId: "",
        Price: "",
      },
      //更新表单信息
      updateForm: {
        orderId: 0,
        userId: "",
        orderId: "",
        clothesId: "",
        utilId: "",
        Price: "",
      },
      //表单验证
      addFormrules: {
        userId: [
          { required: true, message: "请输入用户id", trigger: "blur" },
          { min: 1, max: 5, message: "长度在1到5个字符", trigger: "blur" },
        ],
        fieldId: [
          { required: true, message: "请输入雪场id", trigger: "blur" },
          { min: 1, max: 5, message: "长度在1到5个字符", trigger: "blur" },
        ],
        clothesId: [
          { required: true, message: "请输入服装id", trigger: "blur" },
          { min: 1, max: 5, message: "长度在1到5个字符", trigger: "blur" },
        ],
        utilId: [
          { required: true, message: "请输入工具id", trigger: "blur" },
          { min: 1, max: 5, message: "长度在1到5个字符", trigger: "blur" },
        ],
      },
      //修改表单验证
      updateFormrules: {
        userId: [
          { required: true, message: "请输入用户id", trigger: "blur" },
          //{ min: 0, max: 5, message: "长度在1到5个字符", trigger: "blur" },
        ],
        fieldId: [
          { required: true, message: "请输入雪场id", trigger: "blur" },
          //{ min: 0, max: 5, message: "长度在1到5个字符", trigger: "blur" },
        ],
        clothesId: [
          { required: true, message: "请输入服装id", trigger: "blur" },
          //{ min: 0, max: 5, message: "长度在1到5个字符", trigger: "blur" },
        ],
        utilId: [
          { required: true, message: "请输入工具id", trigger: "blur" },
          //{ min: 0, max: 5, message: "长度在1到5个字符", trigger: "blur" },
        ],
      },
      //用户角色
      roleStyle: false,
    };
  },
  created() {
    this.allOrder();
  },
  methods: {
    //获取全部订单信息
    async allOrder() {
      //调用post
      const { data: res } = await this.$http.get("allOrder", {
        params: this.queryInfo,
      });
      this.orderList = res.data; //将返回数据赋值
      this.total = res.numbers;
    },
    //最大数
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.allOrder();
    },
    //pageNum的触发动作
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.allOrder();
    },

    //监听添加订单的操作
    addDialogClosed() {
      this.$refs.addFormRef.resetFields();
    },
    updateDialogClosed() {
      this.$refs.updateFormRef.resetFields();
    },
    addOrder() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post("addOrder", this.addForm);
        if (res != "success") return this.$message.error("操作失败");
        this.$message.success("操作成功");
        this.addDialogVisible = false;
        this.allOrder();
      });
    },
    //根据主键删除场地信息
    async deleteOrder(orderId) {
      const confirmResult = await this.$confirm(
        "此操作将永久删除用户，是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (confirmResult != "confirm") {
        return this.$message.info("已取消删除");
      }
      const { data: res } = await this.$http.delete(
        "deleteOrder?orderId=" + orderId
      );
      if (res != "success") {
        return this.$message.error("删除失败");
      }
      this.$message.success("删除成功");
      this.allOrder();
    },
    updateVisible(orderInfo) {
      (this.updateDialogVisible = true),
        (this.updateForm.userId = orderInfo.userId),
        (this.updateForm.fieldId = orderInfo.fieldId),
        (this.updateForm.clothesId = orderInfo.clothesId),
        (this.updateForm.utilId = orderInfo.utilId);
      this.updateForm.orderId = orderInfo.orderId;
    },
    async updateOrder() {
      this.$refs.updateFormRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post(
          "updateOrder",
          this.updateForm
        );
        if (res != "success") return this.$message.error("操作失败");
        this.$message.success("操作成功");
        this.updateDialogVisible = false;
        this.allOrder;
      });
    },
  },
};
</script>
<style lang="less" scoped>
.el-breadcrumb {
  margin-bottom: 15px;
  font-size: 17px;
}
</style>
